﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models.ViewPages;

namespace LeadVinaglue.Repository
{
    public class SupportOnlineManagementRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();
        
        public SupportOnlineManagementModel GetModel(string pageType, string roleName)
        {
            SupportOnlineManagementModel model = new SupportOnlineManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListSupportOnline = GetAll();

            return model;
        }

        public SupportOnlineManagementModel GetModelAdd(string pageType, string roleName)
        {
            SupportOnlineManagementModel model = new SupportOnlineManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);

            return model;
        }

        public SupportOnlineManagementModel GetModelEdit(string pageType, string roleName, int id)
        {
            SupportOnlineManagementModel model = new SupportOnlineManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);

            if (id > 0)
            {
                model.SupportOnlineInfo = GetSingle(id);
            }

            return model;
        }

        public SupportOnlineManagementModel GetModelDelete(string pageType, string roleName)
        {
            SupportOnlineManagementModel model = new SupportOnlineManagementModel();
            model.ListSupportOnline = GetAll();

            return model;
        }

        public List<SupportOnline> GetAll()
        {
            return (from c in entities.SupportOnlines
                         orderby c.DateCreated descending
                         select c).ToList();
        }

        public SupportOnline GetSingle(int id)
        {
            return (from _object in entities.SupportOnlines where _object.ID == id select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] SupportOnline _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToSupportOnlines(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.SupportOnlines where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] SupportOnline _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.SaveChanges();
        }
    }
}